package UI;

import User.ScoreBoard;
import TetrisGame.Logic;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Userinterface extends JFrame {
    private JTextField usernameField;
    private JButton loginButton, registerButton;
    private ScoreBoard scoreBoard; // 使用ScoreBoard来管理分数
    private Logic tetrisGame; // 添加一个引用来保存Tetris游戏实例

    public Userinterface(Logic tetrisGame) {
        this.tetrisGame = tetrisGame;
        this.scoreBoard = new ScoreBoard(); // 初始化ScoreBoard
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // 设置布局管理器，添加水平和垂直间隔

        setTitle("用户登录");

        // 创建用户名标签和输入框，放在同一行
        JLabel usernameLabel = new JLabel("用户名:");
        usernameField = new JTextField(15); // 缩小输入框宽度

        // 创建面板，用于放置用户名标签和输入框
        JPanel usernamePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        usernamePanel.add(usernameLabel);
        usernamePanel.add(usernameField);

        // 创建登录和注册按钮，放在同一行
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        loginButton = new JButton("登录");
        registerButton = new JButton("注册");
        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);

        // 添加组件到窗口中
        add(usernamePanel); // 添加用户名面板
        add(buttonPanel); // 添加按钮面板

        setSize(300, 300); // 调整窗口大小
        setLocationRelativeTo(null); // 设置窗口居中显示

        // 登录按钮的事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText().trim();
                if (username.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空");
                    return;
                } else if (scoreBoard.getScore(username) != null) {
                    tetrisGame.setCurrentUser(username);
                    showMainInterface();
                } else {
                    JOptionPane.showMessageDialog(null, "用户不存在，请注册");
                }
            }
        });

        // 注册按钮的事件监听器
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText().trim();
                if (username.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空");
                    return;
                } else if (scoreBoard.getScore(username) == null) {
                    scoreBoard.addScore(username, 0);
                    JOptionPane.showMessageDialog(null, "注册成功");
                    showMainInterface();
                } else {
                    JOptionPane.showMessageDialog(null, "用户名已存在");
                }
            }
        });

        // 使用addWindowListener添加窗口关闭事件处理
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                saveScoresToFile();
                System.exit(0); // 退出程序
            }
        });

        setVisible(true);
    }

    private void saveScoresToFile() {
        scoreBoard.saveScores();
    }

    private void showMainInterface() {
        dispose(); // 关闭登录窗口
        new MainInterface(this, tetrisGame, scoreBoard);
    }

//    public Integer getScore(String username) {
//        return scoreBoard.getScore(username);
//    }

    public String getCurrentUsername() {
        return usernameField.getText().trim();
    }
}